package com.aube.plugin.figure;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.SysUtil;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/5/17.
 */
public class FigureItem extends DLLayout {

    private Context context;
    private IPositionManager mManager;

    private AtomicBoolean mAnimated = new AtomicBoolean(false);

    private TextView mText;
    private TextView mDes, mPlayed;
    private LinearLayout mImgGroup;
    private ImageView mImg;
    private View mDesGroup;

    private int mWidth;

    public FigureItem(Context context) {
        this(context, null);
    }
    public FigureItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public FigureItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    public void initViews() {
        inflate(R.layout.actor_item, this, true);

        mDesGroup = findViewById(R.id.actor_des_group);
        mText = (TextView) findViewById(R.id.actor_title);
        mPlayed = (TextView) findViewById(R.id.actor_played);
        mDes = (TextView) findViewById(R.id.actor_desc);
        mImgGroup = (LinearLayout) findViewById(R.id.actor_img_group);
        mImg = (ImageView) findViewById(R.id.actor_img);
    }

    @Override
    public void fillData(Object obj) {
        mWidth = SysUtil.getAbsoluteScreenWidth(context);
        int space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        int itemWidth = (mWidth-4*space)/2*9/16;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgGroup.getLayoutParams();
        params.width = itemWidth;
        params.height = itemWidth;
        mImgGroup.setLayoutParams(params);

        if(obj == null || !(obj instanceof LinkedHashMap))
            return;
        final Character cItem = new Character();
        LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
        Iterator<String> keyIterator = lhm.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            Object value = lhm.get(key);

            if(Character.ACTOR_NAME.equalsIgnoreCase(key)) {
                cItem.actorName = (String) value;
            } else if(Character.ACTOR_PIC.equalsIgnoreCase(key)) {
                cItem.actorPic = (String) value;
            } else if("actorDesc".equalsIgnoreCase(key)) {
                cItem.actorDesc = (String) value;
            } else if("guestName".equalsIgnoreCase(key)) {
                cItem.actorName = (String) value;
            } else if("guestPic".equalsIgnoreCase(key)) {
                cItem.actorPic = (String) value;
            } else if(Character.PRAISE_COUNT.equalsIgnoreCase(key)) {
                cItem.praiseCount = (int) ((double) value);
            } else if(Character.RELATED_ID.equalsIgnoreCase(key)) {
                cItem.relatedId = (String) value;
            } else if("guestDesc".equalsIgnoreCase(key)) {
                cItem.actorDesc = (String) value;
            } else if("praised".equalsIgnoreCase(key)) {
                cItem.praised = (String) value;
            } else if("period".equalsIgnoreCase(key)) {
                cItem.period = (String) value;
            } else if("isNotClick".equalsIgnoreCase(key)) {
                cItem.isNotClick = (String) value;
            }
        }

        setText(mText, cItem.actorName);
        setText(mDes, cItem.actorDesc);
        setText(mPlayed, cItem.period);
        ImageLoader.getInstance(context).fillRoundImage(mImg, cItem.actorPic, 200, 200);
        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBridge != null && cItem.canClick()) {
//                    mBridge.onClickToCharacter(cItem);
                    DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
                    intent.putExtra(CharacterActivity.ACTOR, cItem);
                    mBridge.openPluginActivity(intent);
                }
            }
        });
    }

    private void setText(TextView textView, String txt) {
        if(StringUtils.isNotBlank(txt)) {
            textView.setText(txt);
            textView.setVisibility(View.VISIBLE);
        } else
            textView.setVisibility(View.GONE);
    }

    @Override
    public void notifyAnim(int animDelay) {
        if(mManager == null)
            return;
        if(mAnimated.get())
            return;
        mAnimated.set(true);
        setVisibility(View.VISIBLE);
//        ObjectAnimator anim = ObjectAnimator.ofFloat(this, "translationX", getWidth(), 0);
//        anim.setDuration(300);
//        anim.start();

        mImgGroup.setAlpha(0f);
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(mImgGroup, "alpha", 0f, 1f);
        alphaAnim.setDuration(400);

        mDesGroup.setAlpha(0f);
        ObjectAnimator desTranslateAnim = ObjectAnimator.ofFloat(mDesGroup, "translationX", mWidth*1/3, 0);
        desTranslateAnim.setDuration(400);

        ObjectAnimator desAlphaAnim = ObjectAnimator.ofFloat(mDesGroup, "alpha", 0f, 1f);
        desAlphaAnim.setDuration(400);

        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(alphaAnim, desAlphaAnim, desTranslateAnim);
        animSet.start();
    }

    @Override
    public void registToManager(IPositionManager manager) {
        if(manager == null)
            return;
        mAnimated.set(false);
        manager.add(this);
        this.mManager = manager;
        setVisibility(View.INVISIBLE);
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
        if(mManager == null || manager == null)
            return;
        manager.remove(this);
        this.mManager = null;
    }

    @Override
    public int[] getLocations() {
        int[] locs = new int[2];
        getLocationOnScreen(locs);
        return locs;
    }

}